//
//  CitySelectViewController.m
//  团购
//
//  Created by Jones on 11/3/16.
//  Copyright © 2016 Jones. All rights reserved.
//

#import "MenutbVC.h"
#import "../MKConstant.h"
//#import "MyDataSource.h"
#import "../DB/DataPersist.h"
#import "../ItemViewController.h"
#define SECTION_HEADER_HEIGHT 50

//#import "HYBSectionModel.h"
//#import "HYBHeaderView.h"

//static NSString *kCellIdentfier = @"UITableViewCell";
//static NSString *kHeaderIdentifier = @"HeaderView";

@interface MenutbVC ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *indexArray;
    //NSArray *cityArray;
    UITableView *tableView;

    NSMutableArray *menuarr;
    
    ItemViewController *itemVc;
    
   
}
//@property (nonatomic, strong) UITableView *tableView;
//@property (nonatomic, strong) NSMutableArray *sectionDataSources;
@end

@implementation MenutbVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setMenuData];
    
    //读取文件
//    NSString *path = [[NSBundle mainBundle]pathForResource:@"city.plist" ofType:nil];
//    cityArray = [NSArray arrayWithContentsOfFile:path];
    
    tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, UISCREEN_WIDTH, UISCREEN_HEIGHT-44)];
    [self.view addSubview:tableView];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
    indexArray = [[NSMutableArray alloc]init];
    for (NSArray *array in menuarr) {
        NSDictionary *dic = [array firstObject];
        NSString *cate = [dic valueForKey:@"cate"];
        [indexArray addObject:cate];
    }
    //NSLog(@"menuarr %@",menuarr);
    
    //NSLog(@"indexArray %@",indexArray);
   [indexArray addObject:@"3"];  //增加section
   [indexArray addObject:@"4"];
   [indexArray addObject:@"5"];
   // NSLog(@"indexArray %@",indexArray);
    
    
//    indexArray = [[NSMutableArray alloc]init];
//    for (NSArray *array in cityArray) {
//        NSDictionary *dic = [array firstObject];
//        NSString *cityLetter = [dic valueForKey:@"cityLetter"];
//        [indexArray addObject:cityLetter];
//    }
    
    //y头部view
//    UIView *view_tbHead = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 90)];
//    UILabel *lab_hotCity = [[UILabel alloc]initWithFrame:CGRectMake(44, 0, UISCREEN_WIDTH-44, 5)];
//    lab_hotCity.text = @"---热门城市---";
//    lab_hotCity.font = [UIFont systemFontOfSize:12.0];
//    lab_hotCity.textAlignment = NSTextAlignmentLeft;
//    [view_tbHead addSubview:lab_hotCity];
//
//    NSArray *array_hotCity = @[@"北京",@"上海",@"深圳",@"广州",@"杭州",@"武汉"];
//    for (int i = 0; i<6; i++) {
//        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20+(i%3)*(UISCREEN_WIDTH/3-10), (i/3)*40+15, UISCREEN_WIDTH/3-20, 35)];
//        NSString *cityName = [array_hotCity objectAtIndex:i];
//        label.text = cityName;
//        label.font = [UIFont systemFontOfSize:18.0];
//        label.textAlignment = NSTextAlignmentCenter;
//        label.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0];
//        [view_tbHead addSubview:label];
//    }
//    tableView.tableHeaderView = view_tbHead;
}
-(void)viewWillAppear:(BOOL)animated{

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return indexArray.count; //menuarr
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0 || section==1) {
        NSArray *item = [menuarr objectAtIndex:section];
        //NSLog(@"item.count %lu",(unsigned long)item.count);
        return item.count;
    } else {
        return 0;
    }
   
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"menuCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    NSArray *array = [menuarr objectAtIndex:indexPath.section];
    //NSLog(@"indexPath.row %ld",(long)indexPath.row);
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    //NSLog(@"cell dic %@",dic);
    
    NSString *subtitle = [dic valueForKey:@"title"];
    cell.textLabel.text = subtitle;
    return cell;
}
// 分组

//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    NSArray *array_hotCity = @[@"北京",@"上海",@"深圳",@"广州",@"杭州",@"武汉"];
//    NSString *str = [array_hotCity objectAtIndex:section]; //indexArray
//    NSLog(@"header title %@",str);
//    return str;
//}

// 右侧索引
//-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//    return indexArray;
//}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,UISCREEN_WIDTH,SECTION_HEADER_HEIGHT)];
    v.tag = section;
    // UILabel *l = [[UILabel alloc] initWithFrame:   ]
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleSection:)];
    [v addGestureRecognizer:tap];
    [v setBackgroundColor:[UIColor whiteColor]];
    
    NSArray *hadertitlearr = @[@"皇牌服務",@"全方位健康管理",@"promotion",@"關於Young+",@"聯絡Young+"];
    NSString *str = [hadertitlearr objectAtIndex:section]; //indexArray
    NSLog(@"header title %@",str);
   
    UILabel *label =  [[UILabel alloc]initWithFrame:CGRectMake(0 , 0,UISCREEN_WIDTH, 50)];
    label.textAlignment = NSTextAlignmentCenter; //UITextAlignmentCenter 字體居中
    label.textColor = MENUBROWN;
    label.text = str;
    [v addSubview:label];
    
    return v;
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return SECTION_HEADER_HEIGHT;
}
-(void) toggleSection:(UITapGestureRecognizer *)tap {
    UIView *v = tap.view;
    if (v.tag==0 || v.tag==1) {
        //toggle section cells
        NSLog(@"Section chick have Details");
    } else {
        NSLog(@"Section chick only");
    }
}



//被选择行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array = [menuarr objectAtIndex:indexPath.section];
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    NSString *str = [dic valueForKey:@"title"];
    NSLog(@"didSelectRow %@",str);
    //[[MyDataSource getInstance]setCityName:str];
    
    //NSString *itemDataSource = [dicrow valueForKey:@"id"];title
    //NSString *itemDataSource = [dicrow valueForKey:@"title"];
    NSString *itemDataSource = [[NSString alloc] init];
    //NSNumber *B = [NSNumber numberWithInt:123456];
    NSNumber *B = [dic valueForKey:@"detail"];
    itemDataSource = [NSString stringWithFormat:@"%@",B];
    NSLog(@"itemDataSource  %@",itemDataSource);
    
    //[self.navigationController popViewControllerAnimated:true];
    
    itemVc = [[ItemViewController alloc]init];
    itemVc.itemDatasource = itemDataSource;
    [self.navigationController pushViewController:itemVc animated:true];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setMenuData{
    //读取数组值
    NSMutableArray *MACArray = [[DataPersist readValueNSMutableArray:@"homeindex"] mutableCopy];
    //NSLog(@"MACArray %@ ",MACArray);
    NSMutableArray *dataarr = [MACArray valueForKey:@"data"];
    //NSLog(@"dataarr is %@",dataarr);
    
    
    //字典   包含数组
    NSMutableArray *aceServicesarr = [dataarr valueForKey:@"aceServices"];
    NSArray *arra = [self setnewarr:aceServicesarr cate:@"aceServices"];
    //NSLog(@"arra %@",arra);
    
    NSMutableArray *healthManagementarr = [dataarr valueForKey:@"healthManagement"];
    NSArray *arrb = [self setnewarr:healthManagementarr cate:@"healthManagement"];
    
    
    NSMutableArray *menuarr2 = [[NSMutableArray alloc]init]; //菜单数组新建
    [menuarr2 addObject:arra];
    [menuarr2 addObject:arrb];
    //NSLog(@"menuarr.count %lu",(unsigned long)menuarr2.count);
    
    menuarr = [menuarr2 mutableCopy];
    //NSLog(@"menuarr.count %lu",(unsigned long)menuarr.count);
    //NSLog(@"menuarr %@",menuarr2);
}

-(NSMutableArray*)setnewarr:(NSMutableArray *)Mutbarr cate:(NSString*)cate{
    
    NSMutableArray * outputarr = [NSMutableArray array];
    
    for (NSMutableDictionary *dicset in Mutbarr) {  //循环。z将数组中值 循环到新数组
        
        //NSString *detail = [dicset valueForKey:@"detail"];
        //NSLog(@"detail %@",detail);
        
        NSMutableDictionary * mFirstDic = [[NSMutableDictionary alloc] init];
        NSArray *array1 = [dicset allKeys];
   
        [mFirstDic setValue:cate forKey:@"cate"];  //设置该分类名称
        
        
        for (int i = 0; i < array1.count; i++) {
            [mFirstDic setValue:[dicset valueForKey:@"detail"] forKey:@"detail"];
            [mFirstDic setValue:[dicset valueForKey:@"id"] forKey:@"id"];
            [mFirstDic setValue:[dicset valueForKey:@"img"] forKey:@"img"];
            [mFirstDic setValue:[dicset valueForKey:@"title"] forKey:@"title"];
        }
        //NSLog(@"mFirstDic %@",mFirstDic);
        
        [outputarr addObject:mFirstDic];
    }
    
    return outputarr;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
